package cn.snmp.mutual.adapter.impl;

import cn.snmp.mutual.nms.SnmpString;
import cn.snmp.mutual.nms.SnmpVar;
import cn.snmp.mutual.adapter.GeneralMibNodeAdapter;
import cn.snmp.mutual.data.ObjService;

/**
 * @author dengkn
 * MIB NODE Adapter ，extends GeneralMibNodeAdapter
 * 在读取和写入snmp mib node 的时候实现readMibData、writeMibData方法
 */
public class InetAddressAdapter extends GeneralMibNodeAdapter {
    @Override
    public String readMibData(SnmpVar value, ObjService os) {
        if (value instanceof SnmpString) {
            byte[] ipadd = ((SnmpString) value).toBytes();
            String val = "";
            for (int i = 0; i < ipadd.length; i++)
                val += " " + ((ipadd[i] + 256) % 256);
            val = val.trim();
            val = val.replace(' ', '.');
            return val;
        }
        return "";
    }

    @Override
    public SnmpVar writeMibData(String value, ObjService os) {
        String[] ips = value.split("\\.");
        byte[] add = new byte[ips.length];
        for (int i = 0; i < ips.length; i++) {
            add[i] = (byte) (Integer.parseInt(ips[i]));
        }
        SnmpVar var = (SnmpVar) new SnmpString(add);
        return var;
    }

}
